const app = getApp()
Page({
    data: {
        tokenID: '',
        originalPassword: '',
        newPassword: '',
        confirmPasswordvalue: '',
        warning: '',
        submitType: false,
    },
    onLoad: function(options) {
        var _this = this;
        _this.setData({
            tokenID: app.data.tokenID
        })
    },
    originalPassword: function(event) {
        var _this = this;
        _this.setData({
            originalPassword: event.detail.value
        })
    },
    biuroriginal: function(event) {
        var _this = this;
        if (_this.data.originalPassword == '    ') {
            _this.setData({
                submitType: false,
                warning: '请输入原始密码'
            })
        } else {
            _this.setData({
                warning: ''
            })
        }
    },
    newPassword: function(event) {
        var _this = this;
        var pwdReg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/;
        _this.setData({
            newPassword: event.detail.value
        })
        if (_this.data.newPassword != _this.data.confirmPasswordvalue) {
            _this.setData({
                warning: '新密码于确认密码不一致，请重新输入。',
                submitType: false
            })
        } else {
            _this.setData({
                warning: '',
                submitType: true
            })
        }
        if (!pwdReg.test(_this.data.newPassword)) {
            _this.setData({
                warning: '必须是6-16个英文字母,数字或符号(除空格),且字母,数字至少包含两种',
                submitType: false
            })
        } else {
            _this.setData({
                warning: ''
            })
        }
    },
    confirmPassword: function(event) {
        var _this = this;
        var pwdReg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/;
        _this.setData({
            confirmPasswordvalue: event.detail.value
        })
        if (_this.data.newPassword != _this.data.confirmPasswordvalue) {
            _this.setData({
                warning: '新密码于确认密码不一致，请重新输入。',
                submitType: false
            })
        } else {
            _this.setData({
                warning: '',
                submitType: true
            })
        }
        if (!pwdReg.test(_this.data.confirmPasswordvalue)) {
            _this.setData({
                warning: '必须是6-16个英文字母,数字或符号(除空格),且字母,数字至少包含两种',
                submitType: false
            })
        } else {
            _this.setData({
                warning: '',
                submitType: true
            })
        }
    },
    submitBtn: function() {
        var _this = this;
        if (_this.data.originalPassword == '') {
            _this.setData({
                warning: '请输入原始密码'
            })
        }
        if (_this.data.newPassword == '') {
            _this.setData({
                warning: '请输入新密码'
            })
        }
        if (_this.data.confirmPasswordvalue == '') {
            _this.setData({
                warning: '请确认新密码'
            })
        }
        if (_this.data.newPassword != _this.data.confirmPasswordvalue) {
            _this.setData({
                warning: '新密码于确认密码不一致，请重新输入。',
                submitType: false
            })
        }
        if (_this.data.submitType == false) {
            _this.setData({
                warning: '新密码于确认密码不一致，请重新输入。'
            })
        }
        _this.setData({
            warning: ''
        })
        wx.request({
            url: app.data.api + 'userinfo/updatePassword',
            data: {
                token: app.data.tokenID,
                old_pwd: _this.data.originalPassword,
                new_pwd: _this.data.confirmPassword
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status) {
                    wx.showModal({
                        title: '提示',
                        showCancel: false,
                        confirmColor: '#04A9F4',
                        content: res.data.msg,
                        success: function(res) {
                            wx.removeStorageSync("token");
                            wx.reLaunch({
                                url: '../login/login'
                            })
                        }
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        showCancel: false,
                        confirmColor: '#04A9F4',
                        content: res.data.msg,
                        success: function(res) {}
                    })
                }
            }
        })
    }
})